package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.CartMapper;
import com.guigu.pojo.Cart;
import com.guigu.pojo.ShopInfo;
import com.guigu.service.CartService;
import com.guigu.service.ShopInfoService;
import com.mysql.fabric.xmlrpc.base.Array;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    ShopInfoService shopInfoService;

    @Autowired
    CartMapper cartMapper;//购物车Mapper

    //显示所有及分页
    @Override
    public Page<Cart> allCart(Cart cart, Integer pageno, Integer pagesize) {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<Cart>();
        queryWrapper.orderByDesc("id");//根据倒叙排列
        if (cart.getUId()!=null){
            queryWrapper.eq("u_id",cart.getUId());
        }
        Page<Cart> page = this.page(new Page<Cart>(pageno, pagesize), queryWrapper);
        for (Cart c : page.getRecords()) {
            c.setShop(shopInfoService.queryShopById(c.getGId()));
        }
        return page;
    }

    @Override
    public Map delCarts(List<Integer> ids) {
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","删除失败");
        boolean b=this.removeByIds(ids);
        if (b){
            map.put("code",1);
            map.put("msg","删除成功");
        }
        return map;
    }

    //根据多个id查询购物车
    @Override
    public List<Cart> queryCartsByIds(List<Integer> cartIds) {
        List<Cart> carts = cartMapper.selectBatchIds(cartIds);
        if(carts.size()>0){
            for (Cart cart : carts) {
                cart.setShop(shopInfoService.queryShopById(cart.getGId()));
            }
        }
        return carts;
    }

    //根据商品id添加一条购物车记录并返回购物车
    @Override
    public Map addCartByShopId(Cart cart) {
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","加入购物车失败");
        //查询商品是重复
        QueryWrapper<Cart> queryWrapper=
                new QueryWrapper<Cart>();
        queryWrapper.eq("u_id",cart.getUId());
        queryWrapper.eq("g_id",cart.getGId());
        Cart one = this.getOne(queryWrapper);
        boolean b=false;
        if (one!=null){
            //查询该商品是否大于上架数量
            ShopInfo shop = shopInfoService.getById(cart.getGId());
            if (shop.getShopUpnum()>one.getQuantity()+1){
                one.setQuantity(one.getQuantity()+1);
                b = this.updateById(one);
            }else {
                map.put("code",2);
                map.put("msg","超出商品库存!");
            }
            cart=one;

        }else{
            b = this.save(cart);
        }
        if (b){
            map.put("code",1);
            map.put("msg","加入成功!在购物车等亲~");
            cart.setShop(shopInfoService.queryShopById(cart.getGId()));
            map.put("cart",cart);
        }
        return map;
    }

    //添加订单计算
    @Override
    public Map changeNum(Cart cart) {
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","数量超出库存");
        Cart byId = this.getById(cart.getId());
        ShopInfo shop = shopInfoService.getById(byId.getGId());
        if (shop.getShopUpnum()>cart.getQuantity()){
            boolean b = this.updateById(cart);
            if (b){
                map.put("code",1);
            }
        }
        return map;
    }

    //根据用户id统计购物车数量
    @Override
    public int querygwCartnum(int uid) {
        int num=0;
        try {
            num=cartMapper.querygwCartnum(uid);
        } catch (Exception e){
            num=0;
        }
        return num;
    }
}
